using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class DeadGround : MonoBehaviour
{
    private Coroutine noDeadTime;
    GameObject[] balls;
    private bool isCanDead = true;
    private bool isCanMiss = true;
    private void Update()
    {
        balls = GameObject.FindGameObjectsWithTag("Ball");
    }
    
    public void NoMissTime()
    {
        if (isCanMiss)
        {
            isCanMiss = false;
            noDeadTime = StartCoroutine(NoDeadTime());
        }
    }

    private IEnumerator NoDeadTime()
    {
        gameObject.GetComponent<SpriteRenderer>().color = new Color(0.047f, 0.388f, 0.537f);
        isCanDead = false;
        yield return new WaitForSeconds(5);
        gameObject.GetComponent<SpriteRenderer>().color = new Color(0.235f, 0.235f, 0.235f);
        isCanDead = true;
        isCanMiss = true;
        noDeadTime = null;
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("Ball") && isCanDead)
        {
            Destroy(collision.gameObject);
            if (balls.Length <= 1)
            {
                FindObjectOfType<GameManager>().MissBall();
            }
        }
    }
}
